import Vue from 'vue';
import VueRouter from 'vue-router';
import {getStore} from "../assets/utils/tools";
import {breadcrumb} from "../assets/utils/breadcrumb";

Vue.use(VueRouter);

import Home from '../views/home/home.vue';

const Login = r => require(['../views/login/login.vue'], r);
const MenuManage = r => require(['../views/menuManage/menuManage.vue'], r);
const UserManage = r => require(['../views/userManage/userManage.vue'], r);
const PersonalCenter = r => require(['../views/personalCenter/personalCenter.vue'], r);
const ArticleManage = r => require(['../views/articleManage/articleManage.vue'], r);
const ArticleDetail = r => require(['../views/articleDetail/articleDetail.vue'], r);
const Comment = r => require(['../views/comment/comment.vue'], r);
const SlideManage = r => require(['../views/slideManage/slideManage.vue'], r);
const TagManage = r => require(['../views/tagManage/tagManage.vue'], r);

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta: {name: '主页', requireLogin: true},
    redirect: '/personalCenter',
    children: [
      {
        path: '/menuManage',
        name: 'menuManage',
        meta: {name: '菜单管理', requireLogin: true},
        component: MenuManage
      },
      {
        path: '/userManage',
        name: 'userManage',
        meta: {name: '用户管理', requireLogin: true},
        component: UserManage
      },
      {
        path: '/personalCenter',
        name: 'personalCenter',
        meta: {name: '个人中心', requireLogin: true},
        component: PersonalCenter
      },
      {
        path: '/articleManage',
        name: 'articleManage',
        meta: {name: '文章管理', requireLogin: true},
        component: ArticleManage
      },
      {
        path: '/articleDetail',
        name: 'articleDetail',
        meta: {name: '文章详情', requireLogin: true},
        component: ArticleDetail
      },
      {
        path: '/comment',
        name: 'comment',
        meta: {name: '评论', requireLogin: true},
        component: Comment
      },
      {
        path: '/slideManage',
        name: 'slideManage',
        meta: {name: '轮播管理', requireLogin: true},
        component: SlideManage
      },
      {
        path: '/tagManage',
        name: 'tagManage',
        meta: {name: '标签管理', requireLogin: true},
        component: TagManage
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta: {name: '登录', requireLogin: false},
    component: Login
  }
];

const router = new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes
});

// breadcrumb(router);
router.beforeEach((to, from, next) => {
  to.matched.some((res) => {
    if (res.meta.requireLogin) {
      if (getStore('token')) {// 判断是否登录
        next()
      } else {
        next({
          path: "/login",
        });
      }
    } else {
      next()
    }
  })
});

export default router
